package com.template.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.template.dto.Comment;
import com.template.result.Result;
import com.template.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/comment/addComment")
    public Result addComment(@RequestBody Comment comment) throws JsonProcessingException {
        return commentService.addComment(comment);
    }

    @GetMapping("/comment/getComment")
    public List<Comment> getCommentsByTimeOrGood(
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "good", required = false) Integer good,
            @RequestParam(value = "userId",required = false) Integer userId) {

        // 创建参数Map
        Map<String, Object> params = new HashMap<>();
        params.put("createTime", createTime);
        params.put("good", good);
        params.put("userId",userId);

        // 查询评论

        return commentService.getCommentsByTimeOrGood(params);
    }

    @PutMapping("/comment/good")
    public Result good(@RequestParam Integer userId,
                       @RequestParam Integer id) throws JsonProcessingException {
        commentService.good(userId,id);
        return Result.success();
    }

    @PutMapping("/comment/noGood")
    public Result noGood(@RequestParam Integer userId,
                       @RequestParam Integer id) throws JsonProcessingException {
        commentService.noGood(userId,id);
        return Result.success();
    }
}
